<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>访客预约</title>
    <link href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="layui-container" style="margin-top: 30px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">访客姓名</label>
            <div class="layui-input-block">
                <input class="layui-input" lay-verify="required" name="visitorName" placeholder="请输入访客姓名"
                       required
                       type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-block">
                <input class="layui-input" lay-verify="required|identity" name="idCard" placeholder="请输入身份证号"
                       required
                       type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input class="layui-input" lay-verify="required|phone" name="phone" placeholder="请输入手机号" required
                       type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">访问日期</label>
            <div class="layui-input-block">
                <input class="layui-input" id="visitDate" lay-verify="required" name="visitDate"
                       placeholder="请选择访问日期"
                       required type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="formSubmit" lay-submit>提交预约</button>
            </div>
        </div>
    </form>
</div>

<script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
<script>
    layui.use(['form', 'laydate', 'jquery'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;

        // 初始化日期选择器
        laydate.render({
            elem: '#visitDate',
            type: 'date',
            min: 0, // 限制最小日期为今天
            trigger: 'click', // 点击触发
            format: 'yyyy-MM-dd', // 日期格式
            done: function (value, date) {
                // 选择日期后的回调
            }
        });

        // 自定义验证规则
        form.verify({
            identity: function (value) {
                if (!/^\d{17}[\dXx]$/.test(value)) {
                    return '请输入正确的身份证号码';
                }
            },
            phone: function (value) {
                if (!/^1[3-9]\d{9}$/.test(value)) {
                    return '请输入正确的手机号码';
                }
            }
        });

        // 监听提交
        form.on('submit(formSubmit)', function (data) {
            console.log(data.field)
            $.ajax({
                type: 'post',
                url: '/visitors/addVisitor',
                contentType: 'application/json',
                data: JSON.stringify({
                    ownerId: sessionStorage.getItem("userId"),
                    visitorName: data.field.visitorName,
                    visitorIdentity: data.field.idCard,
                    visitorPhone: data.field.phone,
                    visitTime: new Date(data.field.visitDate).toISOString(),
                }),
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg(data.msg || '预约成功',);
                    } else {
                        layer.msg(res.msg || '预约失败');
                    }
                },
                error: function (xhr) {
                    layer.msg('请求失败: ' + xhr.statusText);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
